package com.woniu.cq.medical.service.impl;

import com.woniu.cq.medical.controller.form.CheckApplyForm;
import com.woniu.cq.medical.dao.ChargeRecordMapper;
import com.woniu.cq.medical.dao.CheckApplyProjectInfoMapper;
import com.woniu.cq.medical.dao.PrescriptionMapper;
import com.woniu.cq.medical.dao.mymapper.My_CaseMapper;
import com.woniu.cq.medical.dao.mymapper.My_ProjectInfoMapper;
import com.woniu.cq.medical.entity.*;
import com.woniu.cq.medical.dao.CheckApplyMapper;
import com.woniu.cq.medical.service.CheckApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-09-05
 */
@Service
public class CheckApplyServiceImpl extends ServiceImpl<CheckApplyMapper, CheckApply> implements CheckApplyService {
    @Autowired
    private My_CaseMapper myCaseMapper;

    @Autowired
    private My_ProjectInfoMapper myProjectInfoMapper;

    @Autowired
    private CheckApplyMapper checkApplyMapper;

    @Autowired
    private CheckApplyProjectInfoMapper checkApplyProjectInfoMapper;

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    @Autowired
    private ChargeRecordMapper chargeRecordMapper;

    /**
     * 插入检查申请单
     * @param checkApplyForm
     */
    @Override
    @Transactional
    public void insertCheckApply(CheckApplyForm checkApplyForm) {
        //查询出对应的病例ID
        String checkedProject = checkApplyForm.getCheckedProject();
        String patientIdCard = checkApplyForm.getPatientIdCard();
        System.out.println("patientIdCard----------------"+patientIdCard);
        Case aCase = myCaseMapper.selectNotPigeonholeCaseByIdCard(patientIdCard);
        System.out.println("aCase----------------"+aCase);
        Integer caseId = aCase.getCaseId();

        //查询出所有的检查需要的累计费用
        String[] projects = checkedProject.split(" \\| ");
        for (String project : projects) {
            System.out.printf("+++++++++++++++++++"+project);
//            ProjectInfo projectInfo = myProjectInfoMapper.selectProjectInfoByName(project);

        }


        double totalMoney = 0.0;
        for (String project : projects) {
            System.out.printf("+++++++++++++++++++"+project);
            ProjectInfo projectInfo = myProjectInfoMapper.selectProjectInfoByName(project);
            totalMoney += projectInfo.getProjectPrice();
        }
        //新增检查申请单
        CheckApply checkApply = new CheckApply();
        checkApply.setCaseId(caseId);
        checkApply.setCheckTotalMoney(totalMoney);
        checkApply.setCheckChargePayStatus("未支付");
        checkApplyMapper.insert(checkApply);

        //新增检查申请单--检查项目详情中间表
        Integer checkApplyId = checkApply.getCheckApplyId();
        for (String project : projects) {
            ProjectInfo projectInfo = myProjectInfoMapper.selectProjectInfoByName(project);
            Integer projectInfoId = projectInfo.getProjectInfoId();
            CheckApplyProjectInfo checkApplyProjectInfo = new CheckApplyProjectInfo();
            checkApplyProjectInfo.setCheckApplyId(checkApplyId);
            checkApplyProjectInfo.setProjectInfoId(projectInfoId);
            checkApplyProjectInfoMapper.insert(checkApplyProjectInfo);
        }

    }

    @Override
    @Transactional
    public void updateProduct(String passback_params) {
        String [] passbackParams=passback_params.split("-");

        for (int i=0;i<passbackParams.length-1;i++){
            int prescriptionId = Integer.parseInt(passbackParams[i]);
            System.out.println();
            Prescription prescription=new Prescription();
            prescription.setPrescriptionId(prescriptionId);
            prescription.setStatus("已缴费");
            prescriptionMapper.updateById(prescription);
        }
        int patientCardId=Integer.parseInt(passbackParams[passbackParams.length-1]);
        ChargeRecord chargeRecord=new ChargeRecord();
        chargeRecord.setPatientCardId(patientCardId);
        chargeRecord.setChargeRecordCause("处方拿药");
        chargeRecord.setChargeRecordStatus("已缴费");
        chargeRecord.setChargeRecordDate(new Date());
        chargeRecordMapper.insert(chargeRecord);
    }

    @Override
    @Transactional
    public void updateProject(String passback_params) {
        String [] passbackParams=passback_params.split("-");

        for (int i=0;i<passbackParams.length-1;i++){
            int checkApplyId = Integer.parseInt(passbackParams[i]);
            System.out.println();
            CheckApply checkApply=new CheckApply();
            checkApply.setCheckApplyId(checkApplyId);
            checkApply.setCheckChargePayStatus("已缴费");
            checkApplyMapper.updateById(checkApply);
        }
        int patientCardId=Integer.parseInt(passbackParams[passbackParams.length-1]);
        ChargeRecord chargeRecord=new ChargeRecord();
        chargeRecord.setPatientCardId(patientCardId);
        chargeRecord.setChargeRecordCause("项目检查");
        chargeRecord.setChargeRecordStatus("已缴费");
        chargeRecord.setChargeRecordDate(new Date());
        chargeRecordMapper.insert(chargeRecord);
    }

    @Override
    @Transactional
    public void updateRegister(String passback_params) {
        int patientCardId=Integer.parseInt(passback_params);
        ChargeRecord chargeRecord=new ChargeRecord();
        chargeRecord.setPatientCardId(patientCardId);
        chargeRecord.setChargeRecordCause("网上挂号");
        chargeRecord.setChargeRecordStatus("已缴费");
        chargeRecord.setChargeRecordDate(new Date());
        chargeRecordMapper.insert(chargeRecord);
    }


}
